package com.mikhailenko.transportMVC.controller;

import java.util.List;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.mikhailenko.transportMVC.exeption.FirmNotFoundExeption;
import com.mikhailenko.transportMVC.model.Firm;
import com.mikhailenko.transportMVC.service.FirmService;

@Controller
@RequestMapping(value="/firm")
public class FirmController {

	@Autowired
	private FirmService firmService;
	
	private static final Logger log = Logger.getLogger(FirmController.class
			.toString());
	
	@RequestMapping(value="/create", method=RequestMethod.GET)
	public ModelAndView newFirmPage(){
		ModelAndView mav = new ModelAndView("firm-new", "firm", new Firm());
		return mav;
	}
	
	@RequestMapping(value="/create", method=RequestMethod.POST)
	public ModelAndView createNewFirm(@ModelAttribute Firm firm,
			final RedirectAttributes redirectAtributes){
		
		ModelAndView mav = new ModelAndView();
		String message = "Company: "+firm.getName()+" was added successfully";
		
		log.info("Name "+firm.getName());
		log.info("Address "+firm.getAddress());
		log.info("Telephone: "+firm.getTelephone());
		
		firmService.create(firm);
		mav.setViewName("redirect:/firm/list");
		
		redirectAtributes.addFlashAttribute("message", message);
		return mav;
	}
	
	@RequestMapping(value="/list", method=RequestMethod.GET)
	public ModelAndView firmListPage(){
		ModelAndView mav = new ModelAndView("firm-list");
		List<Firm> firmList = firmService.findAll();
		mav.addObject("firmList", firmList);
		return mav;
	}
	
	@RequestMapping(value="/edit/{id}", method=RequestMethod.GET)
	public ModelAndView editFirmPage(@PathVariable Integer id){
		ModelAndView mav = new ModelAndView("firm-edit");
		Firm firm = firmService.findById(id);
		mav.addObject("firm", firm);
		return mav;
	}
	
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
	public ModelAndView editFirm(@ModelAttribute Firm firm,
			BindingResult result,
			@PathVariable Integer id,
			final RedirectAttributes redirectAttributes)
			throws FirmNotFoundExeption {

//		if (result.hasErrors()){
//			ModelAndView mav = new ModelAndView("user-edit");
//			user.setUserID(id);
//			mav.addObject("user", user);
//			return mav;
//		}
		
		firm.setId(id);
		firmService.update(firm);

		ModelAndView mav = new ModelAndView("redirect:/");
		String message = "Firm:"+firm.getName()+" was successfully updated.";

		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}
	
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public ModelAndView deleteUser(@PathVariable Integer id,
			final RedirectAttributes redirectAttributes)
			throws FirmNotFoundExeption {

		ModelAndView mav = new ModelAndView("redirect:/index.jpa");

		Firm firm = firmService.delete(id);
		String message = "The firm " + firm.getName()
				+ " was successfully deleted.";

		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}
	
}
